/*
 * @文件描述: 
 * @版本: 
 * @作者: 周晨阳
 * @Date: 2021-08-17 09:59:19
 */
#include "ADC_Conditioning.h"
#ifdef WIN_SIM
/*
 * @文件描述:示波器ADC前端调理电路（垂直系统）参数设置，及触发电路驱动
 * @版本:
 * @作者: 周晨阳
 * @Date: 2021-01-16 00:15:19
 * @说明
 * 由于触发系统需要设置的参数较少，所以将垂直系统和触发系统写在了一个文件中
 */
#include "ADC_Conditioning.h"
#include <stdio.h>
#include <stdlib.h>
#include "DAC.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
static uint8_t Gain = 1;
static uint8_t dampingFactor = 10;
static float TrigVol = 0;
static uint8_t TrigType;

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/* Public functions ---------------------------------------------------------*/

static void test()
{
  while (1)
  {
    // GPIO_SetBits(_4051_A_PORT, _4051_A_PIN);
    // GPIO_SetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_SetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_SetBits(RELAY_PORT, RELAY_PIN);
  }
}
//初始化
void ADC_Condition_Init(void)
{
}
/**
 * @description: 设置放大器增益倍数
 * @param {uint8_t} gain 放大倍数宏，一定要用宏
 * @return {*}
 */
void setAmpGain(uint8_t gain)
{

  switch (gain)
  {
  case VERTI_GAIN_1: //000
    /* code */
    // GPIO_ResetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_ResetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_ResetBits(_4051_A_PORT, _4051_A_PIN);
    Gain = 1;
    break;
  case VERTI_GAIN_2: //001
    /* code */
    // GPIO_ResetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_ResetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_SetBits(_4051_A_PORT, _4051_A_PIN);
    Gain = 2;
    break;
  case VERTI_GAIN_5: //010
    /* code */
    // GPIO_ResetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_SetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_ResetBits(_4051_A_PORT, _4051_A_PIN);
    Gain = 5;
    break;
  case VERTI_GAIN_10: //011
    /* code */
    // GPIO_ResetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_SetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_SetBits(_4051_A_PORT, _4051_A_PIN);
    Gain = 10;
    break;
  case VERTI_GAIN_20: //100
                      /* code */
    // GPIO_SetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_ResetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_ResetBits(_4051_A_PORT, _4051_A_PIN);
    Gain = 20;
    break;
  case VERTI_GAIN_50: //101
    /* code */
    // GPIO_SetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_ResetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_SetBits(_4051_A_PORT, _4051_A_PIN);
    Gain = 51;
    break;
  case VERTI_GAIN_100: //110
    /* code */
    // GPIO_SetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_SetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_ResetBits(_4051_A_PORT, _4051_A_PIN);
    Gain = 100;
    break;
  case VERTI_GAIN_200: //111
    /* code */
    // GPIO_SetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_SetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_SetBits(_4051_A_PORT, _4051_A_PIN);
    Gain = 200;
    break;
  default: //000
    // GPIO_ResetBits(_4051_C_PORT, _4051_C_PIN);
    // GPIO_ResetBits(_4051_B_PORT, _4051_B_PIN);
    // GPIO_ResetBits(_4051_A_PORT, _4051_A_PIN);
    Gain = 1;
    break;
  }
}
/**
 * @brief : 设置衰减系数，由继电器控制，继电器给1为动作，10倍衰减为默认状态
 * @param {uint8_t} factor 衰减倍数宏，注意，要用宏
 * @return {*}
 */
void setDampFactor(uint8_t factor)
{
  switch (factor)
  {
  case DAMPING_FACTOR_1:
    /* code */
    // GPIO_SetBits(RELAY_PORT, RELAY_PIN);
    dampingFactor = 1;
    break;
  case DAMPING_FACTOR_10:
    /* code */
    // GPIO_ResetBits(RELAY_PORT, RELAY_PIN);
    dampingFactor = 10;
    break;
  default:
    // GPIO_ResetBits(RELAY_PORT, RELAY_PIN);
    dampingFactor = 10;
    break;
  }
}
//提升电位的电压值
#define ADD_VOLTAGE 1.35f
/**
 * @description: 设置触发电平,是0-3.3v那个电压,将直接调用DAC输出
 * @param {float} voltage 0-3.3v 超过范围则会直接限制到最大或最小值
 * @return {*}
 */
void setTrigVoltage(float voltage)
{
  if (voltage > 3.3f)
  {
    TrigVol = 3.3f;
  }
  else if (voltage < 0.0f)
  {
    TrigVol = 0;
  }
  else
  {
    TrigVol = voltage;
  }

  setDAC_Voltage(DAC_1, voltage);
}
/**
 * @brief : 根据当前垂直时基下的触发电压计算出实际DAC输出电压
 * @param {float} voltage
 * @return {float } 实际DAC输出电压
 */
float getActualTrigVoltage(float voltage)
{
  float comp_V;
  comp_V = (voltage / (float)dampingFactor) * (float)Gain + ADD_VOLTAGE;
  return comp_V;
}

/**
 * @brief : 获取放大器实际放大倍数
 * @param {*}
 * @return {uint8_t} 实际的放大倍数，不是宏
 */
uint8_t getAmpGain(void)
{
  return Gain;
}
/**
 * @brief : 获取衰减器的实际衰减倍数
 * @param {*}
 * @return {uint8_t} 实际的衰减倍数，不是宏
 */
uint8_t getDampFactor(void)
{
  return dampingFactor;
}
/**
 * @brief : 获取触发比较电路的实际比较电压
 * @param {*}
 * @return {*}
 */
float getTrigVoltage(void)
{
  return TrigVol;
}
#endif // DEBUG
